package com.gzhryc.shared_device.cdcz.code.services;

import com.gzhryc.common.logging.Logger;
import com.gzhryc.jdbc.MultiDBTools;
import com.gzhryc.shared_device.cdcz.code.dao.CDCZOrderDeviceLogDao;
import com.gzhryc.shared_device.cdcz.code.dao.db.CDCZOrderDeviceLog;

import java.sql.SQLException;
import java.util.Date;

public class CDCZOrderDeviceLogService {

    static Logger log = Logger.getLogger(CDCZOrderDeviceLogService.class);

    public static CDCZOrderDeviceLogService self(Date date) {
        return MultiDBTools.getService(CDCZOrderDeviceLogService.class, date);
    }

    public static CDCZOrderDeviceLogService self(String jdbcKey,Date date) {
        return MultiDBTools.getService(jdbcKey, CDCZOrderDeviceLogService.class, date);
    }

    CDCZOrderDeviceLogDao dao;

    public CDCZOrderDeviceLogService(String jdbcKey,Date date) {
        dao = new CDCZOrderDeviceLogDao(jdbcKey,date);
    }

    public CDCZOrderDeviceLog getByTradeNo(String tradeNo){
        try {
            return dao.getById(tradeNo);
        } catch (SQLException e) {
            log.error(e.getMessage(),e);
        }
        return null;
    }

    public boolean save(CDCZOrderDeviceLog deviceLog){
        CDCZOrderDeviceLog temp = getByTradeNo(deviceLog.getTradeNo());
        if(temp != null) {
            try {
                return dao.updateNotNull(deviceLog) > 0;
            } catch (SQLException e) {
                log.error(e.getMessage(), e);
            }
        }else{
            try {
                return dao.insert(deviceLog) > 0;
            } catch (SQLException e) {
                log.error(e.getMessage(), e);
            }
        }
        return false;
    }


}
